package com.jingyanzi.privilege.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.privilege.dao.SecurityJobsMapper;
import com.jingyanzi.privilege.dao.SecurityOrganizationMapper;
import com.jingyanzi.privilege.domain.JobsDO;
import com.jingyanzi.privilege.domain.OrgDO;
import com.jingyanzi.privilege.domain.result.ResultDO;
import com.jingyanzi.privilege.domain.result.ResultSupport;
import com.jingyanzi.privilege.entity.SecurityJobs;
import com.jingyanzi.privilege.entity.SecurityJobsExample;
import com.jingyanzi.privilege.entity.SecurityJobsExample.Criteria;
import com.jingyanzi.privilege.entity.SecurityOrganization;
import com.jingyanzi.privilege.entity.SecurityOrganizationExample;
import com.jingyanzi.privilege.query.JobsQueryDO;
import com.jingyanzi.privilege.service.IJobService;
import com.jingyanzi.privilege.service.IOrgService;
import com.jingyanzi.privilege.util.BeanUtilsExtends;

@Service("jobService")
public class JobServiceImpl extends BaseServiceImpl implements IJobService {

	@Autowired
	private SecurityJobsMapper jobsMapper;

	@Autowired
	private IOrgService orgService;

	@Autowired
	private SecurityOrganizationMapper organizationMapper;
	

	@Override
	public ResultDO add(JobsDO jobsDO) {

		SecurityJobs jobs = new SecurityJobs();

		ResultSupport result = BeanUtilsExtends.copy(jobs, jobsDO);

		if (!result.isSuccess()) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("对象转换失败");
			result.setSuccess(false);
			return result;
		}

		jobs.setCreatetime(Calendar.getInstance().getTime());
		jobs.setModifitime(jobs.getCreatetime());
		jobs.setStatus(1);
		jobs.setIsDelete(1);

		// 不能在非叶子结点添加数据
		/*
		 * if(orgService.isRoot(jobsDO.getOrgId())) {
		 * 
		 * result.setSuccess(false); return result; }
		 */

		ResultDO res = orgService.get(jobsDO.getOrgId());

		if (!res.isSuccess()) {
			result.setErrorCode(ResultDO.JOB_ADD_ERROR);
			result.setErrorMsg("获取组织机构对应岗位失败");
			result.setSuccess(false);
			return result;
		}
		
		OrgDO orgDO = (OrgDO) res.getModel(ResultSupport.FIRST_MODEL_KEY);
		jobs.setDepartment(orgDO.getNameCode());
		
		int r = -1;
		try {
			r = jobsMapper.insertSelective(jobs);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("添加岗位失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("添加岗位失败");
			result.setSuccess(false);
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, jobs.getId());
		return result;
	}
	
	

	@Override
	public ResultDO remove(Integer id) {

		ResultSupport result = new ResultSupport();

		SecurityJobs jobs = new SecurityJobs();

		jobs.setId(id);
		jobs.setIsDelete(-1);

		int r = -1;

		try {
			r = jobsMapper.updateByPrimaryKeySelective(jobs);
		} catch (Exception e) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("删除岗位失败");
			result.setSuccess(false);
			return result;
		}
		
		if (r < 0) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("删除岗位失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	@Override
	public ResultDO modifi(JobsDO jobsDO) {

		SecurityJobs jobs = new SecurityJobs();

		ResultSupport result = BeanUtilsExtends.copy(jobs, jobsDO);
		if (!result.isSuccess())
			return result;

		/*// 不能在非叶子结点添加数据
		if (orgService.isRoot(jobsDO.getOrgId())) {
			result.setErrorCode(ResultDO.JOB_ADD_ERROR);
			result.setErrorMsg("不能在非叶子结点添加");
			result.setSuccess(false);
			return result;
		}*/
		SecurityOrganization org = organizationMapper.selectByPrimaryKey(jobs.getOrgId());
		if (org == null) {
			result.setErrorCode(ResultDO.JOB_ADD_ERROR);
			result.setErrorMsg("获取组织机构失败");
			result.setSuccess(false);
			return result;
		}
		jobs.setDepartment(org.getNameCode());
	
		int r = -1;
		try {
			r = jobsMapper.updateByPrimaryKeySelective(jobs);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("修改岗位信息失败");
			result.setSuccess(false);
			return result;
		}
		if (r < 1) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("修改岗位信息失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	@Override
	public ResultDO get(Integer id) {

		ResultSupport result = new ResultSupport();

		if (id < 1) {
			result.setSuccess(false);
			return result;
		}

		SecurityJobs jobs = null;

		try {
			jobs = jobsMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("获取岗位信息失败");
			result.setSuccess(false);
			return result;
		}

		if (jobs == null) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("获取岗位信息失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, getJobsDO(jobs));
		return result;
	}
	
	
	@Override
	public ResultDO query2(JobsQueryDO query) {

		ResultSupport result = new ResultSupport();

		List<SecurityJobs> list = null;
		
		List<JobsDO> jobsDOList = null;
	
		SecurityJobsExample example = new SecurityJobsExample();
		Criteria createCriteria = example.createCriteria();
	
		
		if (StringUtils.isNotBlank(query.getName())) {
			createCriteria.andNameLike("%" + query.getName() + "%");
		}
		
		if (query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			long count = jobsMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}
		createCriteria.andIsDeleteEqualTo(1);
		example.setOrderByClause("sortNum asc");
		
		try {
			list = jobsMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("获取岗位信息失败");
			result.setSuccess(false);
			return result;
		}

		jobsDOList = getJobsDOList(list);
		
		result.setModel(ResultSupport.FIRST_MODEL_KEY, jobsDOList);
		
		return result;
	}

	@Override
	public ResultDO query(JobsQueryDO query) {

		ResultSupport result = new ResultSupport();

		SecurityOrganizationExample example1 = new SecurityOrganizationExample();
		
		if (StringUtils.isNotBlank(query.getCode())) {
			example1.createCriteria().andCodeLike(query.getCode() + "%").andIsDeleteEqualTo(1);
		}
		List<SecurityOrganization> orgList = null;
		try {
			orgList = organizationMapper.selectByExample(example1);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorMsg("获取岗位信息失败");
			result.setSuccess(false);
			return result;
		}
	
		List<Integer> orgIds = new ArrayList<Integer>();
		for(SecurityOrganization org : orgList) {
			orgIds.add(org.getId());
		}
		List<SecurityJobs> list = null;
	
		SecurityJobsExample example = new SecurityJobsExample();
		Criteria createCriteria = example.createCriteria();
	
		
		if (StringUtils.isNotBlank(query.getName())) {
			createCriteria.andNameLike("%" + query.getName() + "%");
		}
		
		createCriteria.andOrgIdIn(orgIds).andIsDeleteEqualTo(1);
		if (query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			long count = jobsMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		example.setOrderByClause("sortNum asc");
		
		try {
			list = jobsMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("获取岗位信息失败");
			result.setSuccess(false);
			return result;
		}
	
		result.setModel(ResultSupport.FIRST_MODEL_KEY, getJobsDOList(list));
		
		return result;
	}
	
	/*public ResultDO getJobsSubordinate(int jobsId) {
		
		ResultSupport result = new ResultSupport();
		SecurityJobs jobs = null;
		try {
			jobs = jobsMapper.selectByPrimaryKey(jobsId);
		} catch(Exception e) {
			e.printStackTrace();
			result.setErrorMsg("获取岗位信息失败");
			result.setSuccess(false);
			return result;
		}

		List<SecurityJobs> list = null;
	
		SecurityJobsExample example = new SecurityJobsExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andCodeLike(jobs.getCode() + "%");
		
		try {
			list = jobsMapper.selectByExample(example);
		} catch(Exception e) {
			e.printStackTrace();
			result.setErrorMsg("获取岗位信息失败");
			result.setSuccess(false);
			return result;
		}
		
		result.setModel(ResultSupport.FIRST_MODEL_KEY, getJobsDOList(list));
		
		return result;
		
	}*/

}
